package com.nb.camerademo;

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.nb.camerademo.adapter.CameraAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator on 2024-05-22.
 * <p>
 * 站在顶峰 看世界
 * 落到谷底 思人生
 */
public class TestActivity extends AppCompatActivity {


    private RecyclerView rvCamera;

    private List<String> cameraIdList = new ArrayList<>();

    private CameraAdapter cameraAdapter = null;

    private CameraManager manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        rvCamera = findViewById(R.id.rv_camera);
        manager = (CameraManager) getSystemService(CAMERA_SERVICE);
        try {
            String[] cameraIds = manager.getCameraIdList();
            if (cameraIds.length != 0) {
                cameraIdList = new ArrayList<>(Arrays.asList(cameraIds));
                setCameraAdapter();
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }


    }

    private void setCameraAdapter() {
        if (cameraAdapter == null) {
            cameraAdapter = new CameraAdapter(this, manager, cameraIdList);
            rvCamera.setAdapter(cameraAdapter);
        } else {
            cameraAdapter.notifyDataSetChanged();
        }
    }
}
